package com.example.shop.user.service.impl;

import com.example.shop.mapper.UserBrowseHistoryMapper;
import com.example.shop.pojo.User;
import com.example.shop.pojo.UserBrowseHistory;
import com.example.shop.user.dao.UserBrowsingHistoryDao;
import com.example.shop.user.dto.BrowsingHistory;
import com.example.shop.user.dto.BrowsingHistoryQueryParam;
import com.example.shop.user.service.BrowsingHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BrowsingHistoryServiceImpl implements BrowsingHistoryService {

    private final UserBrowsingHistoryDao browsingHistoryDao;
    private final UserBrowseHistoryMapper browseHistoryMapper;

    public BrowsingHistoryServiceImpl(UserBrowsingHistoryDao browsingHistoryDao, UserBrowseHistoryMapper browseHistoryMapper) {
        this.browsingHistoryDao = browsingHistoryDao;
        this.browseHistoryMapper = browseHistoryMapper;
    }


    @Transactional
    @Override
    public boolean addUserBrowsingHistory(User user, long goodsId) {
        //如果浏览历史已经存在，则更新该浏览时间，而不是插入浏览历史
        int changeRow = browseHistoryMapper.noExInsOrUp(new UserBrowseHistory(user.getUserId(), goodsId));
        if(changeRow>0){
            return true;
        }
        return false;
    }

    @Transactional
    @Override
    public boolean deleteBrowsingHistory(User user, long goodsId) {
        int deleteRow = browseHistoryMapper.delete(user.getUserId(), goodsId);
        if(deleteRow>0){
            return true;
        }
        return false;
    }

    @Override
    public List<BrowsingHistory> obtainBrowsingHistory(User user, BrowsingHistoryQueryParam queryParam) {
        return browsingHistoryDao.selectByUserId(user.getUserId(),queryParam);
    }
}
